package com.sunny.controller;

import com.sunny.common.Const;
import com.sunny.pojo.House;
import com.sunny.pojo.SelectHouseCondition;
import com.sunny.pojo.User;
import com.sunny.service.IFileService;
import com.sunny.service.IHouseService;
import com.sunny.utils.PageInformation;
import com.sunny.utils.PageUtil;
import com.sunny.utils.ServerResponse;
import com.sunny.vo.HouseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
 * @ClassName:
 * @Description:
 * @author: yubao.jin
 * @date: 2018/5/13
 * @version:V1.0
 */
@Controller
@RequestMapping("house")
public class HouseController {
    @Autowired
    IHouseService iHouseService;

    @Autowired
    private IFileService iFileService;

    @RequestMapping("add.do")
    @ResponseBody
    public String add(House house, MultipartHttpServletRequest request,
                      @RequestParam MultipartFile inputfile, HttpSession session) {
        String path = request.getSession().getServletContext().getRealPath("upload");
        String targetFileName = iFileService.upload(inputfile, path);
        String url = targetFileName;
        house.setImage(url);

        User user = (User) session.getAttribute(Const.CURRENT_USER);
        house.setSellerid(user.getId());

        ServerResponse serverresponse = iHouseService.add(house);

        if(serverresponse.isSuccess()) {
            return "1";//添加成功
        }

        return "2";//执行出错
    }

    @RequestMapping("del.do")
    @ResponseBody
    public ServerResponse del(@RequestParam(value = "arr", required = false) String[] id) {
        return iHouseService.del(id);
    }

    @RequestMapping("list.do")
    @ResponseBody
    public PageInformation<House> list(HttpServletRequest request, House house, HttpSession session, @RequestParam(required = false) Integer type) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(type == 1) {
            house.setSellerid(user.getId());
        }

        if(type == 2) {
            if(house.getStatus() == 7) {
                house.setStatus(null);
            }
        }

        String pageNumber = request.getParameter("offset");
        String pageSize = request.getParameter("limit");
        PageUtil pageUtil = PageUtil.getInstance();
        pageUtil.setPageNum(Integer.parseInt(pageNumber) / Integer.parseInt(pageSize) + 1);
        pageUtil.setPageSize(Integer.parseInt(pageSize));

        return iHouseService.list(pageUtil, house);
    }

    @RequestMapping("customerList.do")
    @ResponseBody
    public PageInformation<HouseVo> customerList(HttpServletRequest request, SelectHouseCondition house, HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        String pageNumber = request.getParameter("offset");
        String pageSize = request.getParameter("limit");
        PageUtil pageUtil = PageUtil.getInstance();
        if(pageSize == null && pageNumber == null) {
            pageUtil.setPageNum(1);
            pageUtil.setPageSize(15);
        } else {
            pageUtil.setPageNum(Integer.parseInt(pageNumber) / Integer.parseInt(pageSize) + 1);
            pageUtil.setPageSize(Integer.parseInt(pageSize) * 3);
        }

        return iHouseService.customerList(pageUtil, house, user.getId());
    }

    @RequestMapping("search.do")
    @ResponseBody
    public ServerResponse search(House house) {
        return iHouseService.search(house);
    }

    @RequestMapping("passOrNoPass.do")
    @ResponseBody
    public ServerResponse passOrNoPass(House house) {
        return iHouseService.passOrNoPass(house);
    }
}
